/*包含 n 个接口请求函数的模块
每个函数返回 promise 统一格式化处理
*/
// @ts-ignore
import ajax from './ajax.ts';
import jsonp from 'jsonp'
import {message} from "antd";
// 登陆 外部导入登录接口 引入reqLogin  传入用户对象即可
export const reqLogin = (username, password) =>
  ajax("/login", { username, password }, "POST");
// 添加用户
export const reqAddUser = (user) => ajax("/manage/user/add", user, "POST");
//获取天气
export const reqWeather = (city) => {
  const url = `https://wis.qq.com/weather/common?source=pc&weather_type=forecast_1h&province=河南省&city=信阳市`;
  return new Promise((resolve, reject) => {
    jsonp(url, { param: 'callback' }, (err, data) => {
      // if (!err && data.status === 'success') {
      //   const { weather } = data;
      //   resolve(weather);
      // } else {
      //   message.error('获取天气失败');
      // }

        if(!err && data.status === 200){
            // 获取最近一小时天气信息
            resolve(data.data.forecast_1h)
        }else{
            message.error('获取天气失败')
        }
    })
  })
}
//获取分类
export const reqCategories = (parentId) => {
    // 形参默认值 get 可省略
    return ajax('/manage/category/list', {parentId},'GET')
}
//新增分类
export const addCategory = (categoryName, parentId) => {
    return ajax('/manage/category/add',{categoryName, parentId}, 'POST')
}
//更新分类名称
export const updateCategory = ({categoryId, categoryName}) => {
    return ajax('/manage/category/update',{categoryId, categoryName}, 'POST')
}

//获取商品分页列表
export const reqProducts = (pageNum,pageSize) => {
    return ajax('/manage/product/list', {pageNum,pageSize}, 'GET')
}
//搜索商品分页列表
// 根据商品名称搜索
// export const reqSearchProductsByName = ({pageNum,pageSize,searchName}) => {
//     return ajax('/manage/product/search', {pageNum,pageSize,productName: searchName}, 'GET')
// }
// // 根据商品描述搜索
// export const reqSearchProductsByDesc = ({pageNum,pageSize,searchName}) => {
//     return ajax('/manage/product/search', {pageNum,pageSize,productDesc: searchName}, 'GET')
// }

// searchType productName/productDesc 合并
export const reqSearchProducts = ({pageNum,pageSize,searchName,searchType}) => {
    return ajax('/manage/product/search', {pageNum,pageSize,[searchType]: searchName}, 'GET')
}
//获取分类商品 由分类id获取  接口失效
export const  reqCategory = (categoryId) => {
    return ajax('/manage/category/info', {categoryId}, 'GET')
}

//更新商品状态上下架  接口失效
export const reqUpdateStatus = (productId,status) => {
    return ajax('/manage/product/updateStatus', {productId,status}, 'POST')
}

// 删除指定名称的图片
export const reqDeleteImg = (name) => ajax( '/manage/img/delete', {name}, 'POST')
// 添加或修改商品
export const reqAddOrUpdateProduct = (product) => {
    return ajax('/manage/product' +  (product._id ? '/update' : '/add'), product, 'POST')
}
// 获取所有角色列表
export const reqRoleList = () => {
    return ajax('/manage/role/list')
}
//添加角色
export const reqAddRole = (roleName) => {
    return ajax('/manage/role/add', {roleName},'POST')
}
//更新角色
export const reqUpdateRole = (role) => {
    return ajax('/manage/role/update', role,'POST')
}

// 获取用户列表
export const reqUserList = () => {
    return ajax('/manage/user/list')
}

// 添加或更新用户
export const reqAddOrUpdateUser = (user) => {
    return ajax('/manage/user/'+(user?._id ? 'update' : 'add'), user, 'POST')
}
//删除用户
export const reqDeleteUser = (userId) => {
    return ajax('/manage/user/delete', {userId}, 'POST')
}